<?php
    //  外卖编辑后台处理数据
    require_once "config.php";
    require_once "weixin_info.php";
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        // $id = $_POST['id'];
        $sql = $mysql->query("select * from dyspw_shop_waimai where  uid = {$_SESSION['Member_info'][0]['id']} ");
        $waimai_info = $sql->fetch_all(MYSQLI_ASSOC);
        $mem_sql = $mysql->query("select * from dyspw_member where  id = {$_SESSION['Member_info'][0]['id']} ");
        $mem_info = $mem_sql->fetch_all(MYSQLI_ASSOC);
        $params = array(
            'pack_fee'=> $_POST['pack_fee'],
            'radius' => $_POST['radius'],
            'region' => $_POST['region'],
            'region_is' => $_POST['region_is'],
            'delivery_fee' => $_POST['delivery_fee'],
            'start_time' => $_POST['start_time'],
            'end_time' => $_POST['end_time'],
            'state' => $_POST['state'],
        );
        $params['region_is'] = $params['region_is'] == 'true' ? 1 : 0;
        $params['state'] = $params['state'] == 'true' ? 1 : 0;
        // $params['start_time'] = strtotime($params['start_time']);
        // $params['end_time'] = strtotime($params['end_time']);
        // print_r($params);exit;
        $up_type = '';
        if(!empty($waimai_info)){
            $waimai_up = $mysql->query("UPDATE dyspw_shop_waimai SET  pack_fee='{$params['pack_fee']}', radius='{$params['radius']}', region='{$params['region']}', region_is='{$params['region_is']}', delivery_fee='{$params['delivery_fee']}', start_time='{$params['start_time']}', end_time='{$params['end_time']}', state='{$params['state']}' where uid={$mem_info[0]['id']}");
        }else{
            $up_type = 'add';
            $shop_lat = $mem_info[0]['shop_lng'].','.$mem_info[0]['shop_lat'];
            $waimai_up = $mysql->query("insert into dyspw_shop_waimai (uid,shop_name,shop_img,shop_lat,pack_fee,radius,region,region_is,delivery_fee,start_time,end_time,state) values ('{$mem_info[0]['id']}','{$mem_info[0]['username']}','{$mem_info[0]['avatar']}','{$shop_lat}','{$params['pack_fee']}','{$params['radius']}','{$params['region']}','{$params['region_is']}','{$params['delivery_fee']}','{$params['start_time']}','{$params['end_time']}','{$params['state']}')");



        }
        // print_r($city);
        if($waimai_up){
            $wm_sql = $mysql->query("select * from dyspw_shop_waimai where  uid = {$mem_info[0]['id']} ");
            $waimai_add_sql = $wm_sql->fetch_all(MYSQLI_ASSOC);
            if(!empty($up_type)){
                //添加新外卖商户时，在配送平台创建商户
                require_once "shipping.php";
                $Shipping = new Shipping();
                $city = $Shipping->GetMaiYaTianData('/config/city/','');
                foreach ($city['data'] as $key => $val) {
                    if('苏州' == $val['name']){
                        $city_code = $val['code'];
                    }
                }
                $params = array(
                    'origin_id' =>  $waimai_add_sql[0]['id'],
                    'name'      => $mem_info[0]['username'],
                    'city'      => $city_code,
                    'phone'     => $mem_info[0]['mobile'],
                    'address'   => $mem_info[0]['shop_add'],
                    'longitude' => $mem_info[0]['shop_lng'],
                    'latitude'  => $mem_info[0]['shop_lat'],
                    'category'  => 1,
                    'map_type'  => 1,
                );
                $shop_add = $Shipping->GetMaiYaTianData('/shop/add/',$params);
                if($shop_add['code'] == 1){
                    $mysql->query("UPDATE dyspw_shop_waimai SET  shipping_id='{$shop_add['data']['shop_id']}' where id={$waimai_add_sql[0]['id']}");
                    $result['status'] = 'y';
                    $result['info'] = '编辑成功,即将跳转';
                    exit(json_encode($result));
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '外卖上传创建失败，请联系管理员';
                    exit(json_encode($result));
                }
                exit;
            }
            $result['status'] = 'y';
            $result['info'] = '编辑成功,即将跳转';
            exit(json_encode($result));
        }else{
            $result['status'] = 'n';
            $result['info'] = '编辑失败，请重新提交';
            exit(json_encode($result));
        }
    }
 ?>
